package com.lu.statistics.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lu.statistics.entity.RCustomerCart;
import com.lu.statistics.entity.vo.CartProductPrice;
import com.lu.statistics.mapper.RCustomerCartMapper;
import com.lu.statistics.mapper.RCustomerMapper;
import com.lu.statistics.service.CartTypeService;
import com.lu.statistics.service.CustomerCartService;
import com.lu.statistics.service.DishCategoriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CustomerCartServiceImpl extends ServiceImpl<RCustomerCartMapper, RCustomerCart> implements CustomerCartService {
    private final Map<String, CartTypeService> map = new HashMap<>();
    @Autowired
    public List<CartTypeService> serviceList;
    @Autowired
    private RCustomerCartMapper cartMapper;
    @Autowired
    private RCustomerMapper customerMapper;
    @Autowired
    private DishCategoriesService categoriesService;

    @PostConstruct
    void initMap() {
        serviceList.forEach(item -> map.put(item.getType(), item));
    }

    @Override
    @Transactional
    public void saveOrUpdateCustomerCart(RCustomerCart customerCart) throws Exception {
        RCustomerCart cartInfo = cartMapper.queryCustomerCart(customerCart.getCustomerId(), customerCart.getCategoriesId(), customerCart.getProductId());
        if (cartInfo != null) {
            //商品数量相加
            Integer quantity = cartInfo.getQuantity() + customerCart.getQuantity();
            cartInfo.setQuantity(quantity);
            BigDecimal price = cartInfo.getPrice().add(customerCart.getPrice());
            cartInfo.setUpdateId(customerCart.getCreateId());
            cartInfo.setPrice(price);
            cartInfo.setCartStatus(1);
            cartMapper.updateById(cartInfo);
        } else {
            String productType = customerCart.getProductType();
            //商品分类
            map.get(productType).addCustomerCart(customerCart);
        }


    }

    @Override
    public void updateCustomerCart(RCustomerCart customerCart) {
        cartMapper.updateById(customerCart);
    }

    @Override
    public List<RCustomerCart> queryCustomerListByUserId(Integer userId) {
        return cartMapper.queryCustomerCartList(userId);
    }

    @Override
    public CartProductPrice queryCartProductPrice(Integer userId) {
        CartProductPrice cartProduct = cartMapper.queryCartProductPriceByCreateId(userId);
        List<RCustomerCart> cartList = cartMapper.queryCartProductListByCreateId(userId);
        if (cartList != null && cartList.size() > 0) {
            cartProduct.setCartList(cartList);
            return cartProduct;
        }else {
            CartProductPrice product=new CartProductPrice();
            product.setTotalPrice(new BigDecimal(0));
            product.setDishPrice(new BigDecimal(0));
            product.setDishTotalPrice(new BigDecimal(0));
            product.setLiquorTotal(new BigDecimal(0));
            product.setWeddingTotal(new BigDecimal(0));
            return product;
        }


    }

    @Override
    public void updateCartStatus(RCustomerCart customerCart) {
        cartMapper.updateCustomerCartStatus(customerCart.getId(), customerCart.getCustomerId(), customerCart.getCartStatus());
    }

    @Override
    public void deleteCustomerCart(Integer id) {
        cartMapper.deleteById(id);
    }
}
